<template>
  <div class="singleImg-container-div">
    <template>
      <div class="el-upload-list el-upload-list--picture-card">
        <template v-if="imageUrl.length > 0">
          <li
            class="el-upload-list__item is-success"
            v-for="(item, index) in imageUrl"
            :key="item"
          >
            <el-image
              :src="define.comUrl + item"
              class="el-upload-list__item-thumbnail"
              :preview-src-list="fileList"
              :z-index="10000"
              :ref="'image' + index"
            >
            </el-image>
            <span class="el-upload-list__item-actions">
              <span
                class="el-upload-list__item-preview"
                @click="handlePictureCardPreview('image' + index)"
              >
                <i class="el-icon-zoom-in"></i>
              </span>
              <span
                v-if="!disabled"
                class="el-upload-list__item-delete"
                @click="handleRemove(index)"
              >
                <i class="el-icon-delete"></i>
              </span>
            </span>
          </li>
        </template>
        <div
          v-if="imageUrl.length < limit"
          class="upload_style"
          style="height: 100px; width: 100px"
          @click="uploadPicture('flagImg')"
        >
          <i class="el-icon-plus" style="font-size: 30px"></i>
          <div class="tip_word">{{ tip }}</div>
        </div>
      </div>
    </template>
    <el-dialog
      title="裁切图片"
      v-if="cropperModel"
      :visible.sync="cropperModel"
      :modal-append-to-body="false"
      :append-to-body="true"
      width="1200px"
      center
    >
      <cropper-image
        :Name="cropperName"
        :fixed="fixed"
        :autoCropWidth="autoCropWidth"
        :autoCropHeight="autoCropHeight"
        :fixedBox="fixedBox"
        @uploadImgSuccess="handleUploadSuccess"
        ref="child"
      >
      </cropper-image>
    </el-dialog>
  </div>
</template>

<script>
import CropperImage from "./CropperImage.vue";
export default {
  components: {
    CropperImage,
  },
  name: "UploadImage",
  props: {
    value: {
      type: String,
      default: "",
    },
    modular: {
      type: String,
      default: "system",
    },
    type: {
      type: String,
      default: "annex",
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    limit: {
      type: Number,
      default: 1,
    },
    autoCropWidth: {
      type: Number,
      default: 300,
    },
    autoCropHeight: {
      type: Number,
      default: 200,
    },
    fixed: {
      type: Boolean,
      default: false,
    },
    fixedBox: {
      type: Boolean,
      default: false,
    },
    tip: {
      type: String,
      default: "图片上传",
    },
  },
  data() {
    return {
      imageUrl: [],
      uploadHeaders: { Authorization: this.$store.getters.token },
      cropperName: "",
      cropperModel: false,
    };
  },
  computed: {
    fileList() {
      let list = [];
      this.imageUrl.forEach((item) => {
        list.push(this.define.comUrl + item);
      });
      return list;
    },
  },
  watch: {},
  created() {},
  methods: {
    uploadPicture(name) {
      this.cropperName = name;
      this.cropperModel = true;
    },
    //图片上传成功后
    handleUploadSuccess(obj) {
      console.log(obj, "------");
      this.imageUrl.push(obj.url);
      console.log(this.imageUrl, "1-+========");
      this.$emit("input", this.imageUrl);
      this.cropperModel = false;
    },

    handleSuccess(res, file) {
      if (res.code == 200) {
        this.imageUrl.push(res.data.url);
        // this.$emit("input", res.data.url);
      } else {
        this.$message({ message: res.msg, type: "error", duration: 1500 });
      }
    },
    handlePictureCardPreview(refName) {
      console.log(this.$refs[refName], "-----");
      this.$refs[refName] && this.$refs[refName][0].clickHandler();
    },
    handleRemove(index) {
      // this.$emit("input", "");
      // this.imageUrl = []
      this.imageUrl.splice(index, 1);
    },
  },
};
</script>
<style lang="scss" scoped>
.singleImg-container-div {
  // width: 100px;
  height: 100px;
  cursor: pointer;
  .img-uploader {
    >>> .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;

      &:hover {
        border-color: #409eff;
      }
    }
  }

  .icon-box {
    position: relative;

    .upload-tip {
      position: absolute;
      font-size: 14px;
      color: #8c939d;
      left: 0;
      right: 0;
      padding: 0 5px;
      bottom: 10px;
      line-height: 20px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      word-break: break-all;
    }
  }

  .img-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 80px;
    text-align: center;
  }

  .el-upload-list {
    // width: 100px;
    height: 100px;
    // display: block;
    float: left;
    // margin-right: 20px;
    .el-upload-list__item {
      margin-right: 20px;
    }
    .el-upload-list__item,
    .el-image {
      width: 100px;
      height: 100px;
      float: left;
    }
  }
}
.upload_style {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  // display: inline-block;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  border: 1px dashed #cccccc;
  border-radius: 5px;
  overflow: hidden;
  box-shadow: 0 0 1px #cccccc;
  position: relative;
  float: left;
  margin-right: 20px;
}
.upload_style:hover {
  border: 1px solid #69b7ed;
}
.upload_style i {
  margin: 5px;
}
.el-icon-plus {
  color: #8c939d;
  position: relative;
  top: -8px;
}
.tip_word {
  display: block;
  position: absolute;
  top: 63px;
  color: #8c939d;
}
</style>
